package org.mclll.xwangplugin.cartography.recipes;

import de.tr7zw.nbtapi.NBT;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Arrays;
import java.util.List;

/**
 * @author llB
 * @date 2024-06-15 16:57
 * @description todo
 */
public class CartographyTransferReelMainRecipes extends CartographyCustomRecipes {
    /**
     * 判断是否是空卷轴
     * @param item -
     * @return -
     */
    @Override
    protected boolean isMainMaterial(ItemStack item) {
        // 这里代码可以直接复用
        return new CartographyTransferReelCopyRecipes().isViceMaterial(item);
    }

    @Override
    protected boolean isResultItem(ItemStack item) {
        return false;
    }

    /**
     * 判断是否是下届之星
     * @param item -
     * @return -
     */
    @Override
    protected boolean isViceMaterial(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }
        List<Component> lore = item.lore();
        if (lore == null || lore.size() < 3) {
            return false;
        }
        Component component = lore.get(2);
        if (component instanceof TextComponent) {
            String content = ((TextComponent) component).content();
            return content.startsWith("由下界之星");
        }
        return false;
    }

    @Override
    protected ItemStack generalResultItem(Inventory cartographyInventory) {
        ItemStack result = new ItemStack(Material.GLOBE_BANNER_PATTERN);
        NBT.modify(result, nbt -> {
            nbt.setInteger("CustomModelData", 75026);
        });
        Location cartographyLocation = cartographyInventory.getLocation();
        String worldName = cartographyLocation.getWorld().getName();
        String pos = String.format("%.1f,%.1f,%.1f", cartographyLocation.getX(), cartographyLocation.getY(), cartographyLocation.getZ());
        ItemMeta itemMeta = result.getItemMeta();
        itemMeta.displayName(Component.text("传送卷轴·正本").color(NamedTextColor.GREEN).decoration(TextDecoration.ITALIC, false));
        itemMeta.lore(Arrays.asList(
                        Component.text("消耗品").color(NamedTextColor.DARK_GRAY).decoration(TextDecoration.ITALIC, false),
                        Component.text("灵活传送点").color(NamedTextColor.AQUA).decoration(TextDecoration.ITALIC, false),
                        Component.text("---------------------------------").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("一张传送卷轴的正本").color(NamedTextColor.WHITE),
                        Component.text("使用后将传送至卷轴记载的地点").color(NamedTextColor.WHITE),
                        Component.text("可随意复制成多张拓本").color(NamedTextColor.WHITE),
                        Component.text("---------------------------------").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("使用该卷轴的效果：").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("将传送到↓").color(NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, false),
                        Component.text().content("世界：").color(NamedTextColor.DARK_GRAY)
                                .append(Component.text(worldName).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, false))
                                .build(),
                        Component.text(String.format("坐标：%s", pos)).color(NamedTextColor.DARK_GRAY),
                        Component.text("---------------------------------").color(NamedTextColor.WHITE).decoration(TextDecoration.ITALIC, false),
                        Component.text("可在制图台中复制").color(NamedTextColor.GREEN).decoration(TextDecoration.ITALIC, false),
                        Component.text("需下蹲时右键使用").color(NamedTextColor.GREEN).decoration(TextDecoration.ITALIC, false)
                )
        );
        itemMeta.addEnchant(Enchantment.DURABILITY, 1, true);
        result.setItemMeta(itemMeta);
        result.addItemFlags(ItemFlag.HIDE_ITEM_SPECIFICS, ItemFlag.HIDE_ENCHANTS);
        return result;
    }

    @Override
    protected boolean clearMain() {
        return true;
    }

    @Override
    protected boolean clearVice() {
        return true;
    }

}
